package de.hauck.rtmidlet.views;

import java.util.Date;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextField;

import de.hauck.rtmidlet.RTMidlet;
import de.hauck.rtmidlet.model.Configuration;
import de.hauck.rtmidlet.model.StockBean;
import de.hauck.rtmidlet.model.StockStore;

public class FormStockMaintain extends Form implements CommandListener {
	private Command cCancel = new Command("Cancel", Command.BACK, 1);
	private Command cAddStock = new Command("Add", Command.OK, 1);
	private Command cSaveStock = new Command("Save", Command.OK, 1);
	
	//private Display _display;

	private TextField name, symbol_rt, symbol_ts, symbol_yahoo, symbol_ft, amount, invest;
	private DateField date;
	private ChoiceGroup listInChoicegroup;
	private boolean[] selected;
	//private List stockList;

	private StockStore _store;
	private RTMidlet _main;
	//private FormStockMaintainMenu _formMaintainMenu;
	private int _stockNumber;
	private Displayable _dble;

	public FormStockMaintain(int stockNumber,
			StockStore myStore, RTMidlet midlet,
			Displayable dble, int action) {
		super("");
		//_formMaintainMenu = fchange;
		_dble = dble;
		_main = midlet;
		_store = myStore;
	//	_display = display;
		_stockNumber=stockNumber;
		listInChoicegroup = new ChoiceGroup("Show in", ChoiceGroup.MULTIPLE);
		listInChoicegroup.append("Charts RT", null);		
		listInChoicegroup.append("Charts FT", null);
		listInChoicegroup.append("Overview Yahoo", null);

		if (action == 0) {
			setTitle("Add stock");
			name = new TextField("Name", "Nokia", 15, 0);
			symbol_rt = new TextField("Symbol RT", "NOK", 20, 0);
			symbol_ts = new TextField("Symbol TS", "NOK", 20, 0);
			symbol_yahoo = new TextField("Symbol Yahoo", "NOA3.DE", 20, 0);
			symbol_ft = new TextField("Symbol FT", "NOA3.DE", 20, 0);
			amount = new TextField("Amount", "20", 10, 0);
			amount.setConstraints(TextField.NUMERIC);
			invest = new TextField("Invest", "1000", 6, 0);
			invest.setConstraints(TextField.DECIMAL);
			date = new DateField("Buy Date", DateField.DATE);
			date.setDate(new Date(System.currentTimeMillis()));
			
			boolean[] selected = { true, true, true};
			listInChoicegroup.setSelectedFlags(selected);
			append(name);
			append(symbol_rt);
			append(symbol_ts);
			append(symbol_yahoo);
			append(symbol_ft);
			append(amount);
			append(invest);
			append(date);
			append(listInChoicegroup);
			addCommand(cAddStock);
		} else if (action == 1) {
			setTitle("Edit stock");
			StockBean stock = (StockBean) _store.getStocksVector().elementAt(
					_stockNumber);
			name = new TextField("Name", stock.getName(), 15, 0);
			symbol_rt = new TextField("Symbol RT", stock.getSymbolRT(), 20, 0);
			symbol_ts = new TextField("Symbol TS", stock.getSymbolTS(), 20, 0);
			symbol_yahoo = new TextField("Symbol Yahoo",
					stock.getSymbolYahoo(), 20, 0);
			symbol_ft = new TextField("Symbol FT",
					stock.getSymbolFt(), 20, 0);
			amount = new TextField("Amount", stock.getAmount(), 5, 0);
			amount.setConstraints(TextField.NUMERIC);
			invest = new TextField("Invest", stock.getInvest(), 6, 0);
			invest.setConstraints(TextField.DECIMAL);
			date = new DateField("Buy Date", DateField.DATE);
			date.setDate(stock.getBuyDate());
			selected = stock.getDisplayFlags();
			listInChoicegroup.setSelectedFlags(selected);
			deleteAll();
			append(name);
			append(symbol_rt);
			append(symbol_ts);
			append(symbol_yahoo);
			append(symbol_ft);
			append(amount);
			append(invest);
			append(date);
			append(listInChoicegroup);
			addCommand(cSaveStock);
		}
		addCommand(cCancel);
		setCommandListener(this);
		_main.show(this);
	}

	public void commandAction(Command command, Displayable displayable) {
		if (command == cCancel) {
			if (_dble instanceof FormStockMaintainMenu) {
			_main.show(((FormStockMaintainMenu)_dble).getEditStocksMenu());
			} else if (_dble instanceof CanvasSearch) {
				_main.show(_dble);
			}
			//_display.setCurrent(_formMaintainMenu.getEditStocksMenu());
		} else if (command == cSaveStock) {
			StockBean editStock = new StockBean(name.getString(), symbol_rt
					.getString(), symbol_ts.getString(), symbol_yahoo
					.getString(), symbol_ft.getString(), amount.getString(), invest.getString(), date
					.getDate(), getSelectedDisplayFlags());
			
			_store.editStock(editStock, _stockNumber);
			if (_dble instanceof FormStockMaintainMenu) {
				_main.show(((FormStockMaintainMenu)_dble).getEditStocksMenu());
				} else if (_dble instanceof CanvasSearch) {
					_main.show(_dble);
				}
			
			//_display.setCurrent(_formMaintainMenu.getEditStocksMenu());
		} else if (command == cAddStock) {
			StockBean newStock = new StockBean(name.getString(), symbol_rt
					.getString(), symbol_ts.getString(), symbol_yahoo
					.getString(), symbol_ft.getString(), amount.getString(), invest.getString(), date
					.getDate(), getSelectedDisplayFlags());
			
			_store.addStock(newStock);
			if (_dble instanceof FormStockMaintainMenu) {
				_main.show(((FormStockMaintainMenu)_dble).getEditStocksMenu());
				} else if (_dble instanceof CanvasSearch) {
					_main.show(_dble);
				}
			//_main.show(_formMaintainMenu.getEditStocksMenu());
			//_display.setCurrent(_formMaintainMenu.getEditStocksMenu());
		}  
	}

	private boolean[] getSelectedDisplayFlags() {
		boolean[] selected = new boolean[listInChoicegroup.size()];
		for (int i = 0; i < listInChoicegroup.size(); i++) {
			if (listInChoicegroup.isSelected(i))
				selected[i] = true;
			else
				selected[i] = false;
		}
		return selected;
	}
}
